<?php
namespace app\Backend\controller;;
use think\Db;
use think\Request;
use think\Controller;
use app\common\model\Users as UserModel;
class Users extends Common{
    //会员列表
    public function index(){
        $param = request()->param();
        $map = [];
        if(!empty($param['key'])){
            $map["fullName|nicheng|shortName"] = array("like","%".$param['key']."%");
        }
        if(is_numeric($param['uk'])){
            $param['uk'] = intval($param['uk']);
             $map["identity"] = $param['uk'];
        }
        if(is_numeric($param['us'])){
            $param['us'] = intval($param['us']);
             $map["status"] = $param['us'];
        }
        if(is_numeric($param['ut'])){
            $param['ut'] = intval($param['ut']);
            $map["user_type"] = $param['ut'];
        }
        if(!empty($param['date'])){
            list($star,$end) = explode(" - ", $param['date']);
            $map["add_time"] = array('between time',[$star,$end]);
        }
        $userModel = new UserModel;
        $lists = $userModel->where($map)->order('user_id desc')->paginate(array('list_rows'=>$this->pageSize,'page'=>$this->page));
        // $userModel->getLastSql();
        $pageShow = $lists->render();
        $this->assign('lists',$lists);
        $this->assign('userLevel',UserModel::UserLevel());
        $this->assign('status',$userModel->getStatuslabel(false));
        $this->assign('tyles',$userModel->getTypelabel(false));
        $this->assign('pageShow',$pageShow);
        $this->assign('m',$param);
        return $this->fetch();
    }
    public function view($userId=null){
        $userModel = new UserModel;
        $user = UserModel::get($userId);
        if(empty($user)){
            $this->error("用户信息不存在");
        }
        $this->assign('user',$user);
        $this->assign('userLevel',UserModel::UserLevel());
        $this->assign('status',$userModel->getStatuslabel(false));
        $wx = db('weixin_user')->where(array('wxid'=>$user['wid']))->find();
        $this->assign('wx',$wx);
        return $this->fetch();
    }
    public function usersState(){
        $id=input('post.id');
        $status=db('users')->where(array('id'=>$id))->value('is_lock');//判断当前状态情况
        if($status==1){
            $data['is_lock'] = 0;
            db('users')->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 0;
            $result['status'] = 1;
        }else{
            $data['is_lock'] = 1;
            db('users')->where(array('id'=>$id))->setField($data);
            $result['is_lock'] = 1;
            $result['status'] = 1;
        }
        return $result;
    }

    public function edit($id=''){
        if(request()->isPost()){
            $user = db('users');
            $data = input('post.');
            $level =explode(':',$data['level']);
            $data['level'] = $level[1];
            $province =explode(':',$data['province']);
            $data['province'] = $province[1];
            $city =explode(':',$data['city']);
            $data['city'] = $city[1];
            $district =explode(':',$data['district']);
            $data['district'] = $district[1];
            if(empty($data['password'])){
                unset($data['password']);
            }else{
                $data['password'] = md5($data['password']);
            }
            if ($user->update($data)!==false) {
                $result['msg'] = '会员修改成功!';
                $result['url'] = url('index');
                $result['code'] = 1;
            } else {
                $result['msg'] = '会员修改失败!';
                $result['code'] = 0;
            }
            return $result;
        }else{
            $province = db('Region')->where ( array('pid'=>1) )->select ();
            $user_level=db('user_level')->order('sort')->select();
            $info = UsersModel::get($id);
            $this->assign('info',json_encode($info,true));
            $this->assign('title',lang('edit').lang('user'));
            $this->assign('province',json_encode($province,true));
            $this->assign('user_level',json_encode($user_level,true));

            $city = db('Region')->where ( array('pid'=>$info['province']) )->select ();
            $this->assign('city',json_encode($city,true));
            $district = db('Region')->where ( array('pid'=>$info['city']) )->select ();
            $this->assign('district',json_encode($district,true));
            return $this->fetch();
        }
    }


    public function getRegion(){
        $Region=db("region");
        $pid = input("pid");
        $arr = explode(':',$pid);
        $map['pid']=$arr[1];
        $list=$Region->where($map)->select();
        return $list;
    }

    public function usersDel(){
        $map['mchToken'] = $this->mchToken;
        $map['user_id'] = input('user_id');
        if(db('users')->delete($map)){
            return $result = ['code'=>1,'msg'=>'删除成功.','url'=>'/reload'];
        }
         return $result = ['code'=>0,'msg'=>'删除失败.'];
    }
    public function delall(){
        $map['id'] =array('in',input('param.ids/a'));
        db('users')->where($map)->delete();

        $result['msg'] = '删除成功！';
        $result['code'] = 1;
        $result['url'] = url('index');
        return $result;
    }
     /*重置密码*/
    public function retpws(){
        $map = ['user_id'=>$this->request->param('user_id',0)];
        $map['mchToken'] = $this->mchToken;
        $user = db('users')->where($map)->find();
        if(empty($user)){
            $this->error('用户信息不存在.');
        }
        if($this->request->isPost()){
            $data = $this->request->post();
            if(empty($data['user_pasw'])){
               return ['code'=>0,'msg'=>'请输入新密码.'];
            }
            if($data['user_pasw'] != $data['reuser_pasw']){
                return ['code'=>0,'msg'=>'两次输入密码不一致.'];
            }
            if(db('users')->where($map)->update(['password'=>md5($data['user_pasw'])])){
                return ['code'=>1,'msg'=>'密码重置成功.','url'=>"/reload"];
            }
            return ['code'=>0,'msg'=>'密码重置失败.'];
        }
        return $this->fetch();
    }
    /*用户升级*/
    public function userupgrade(){
        $user_id = input('user_id');
        $user = UserModel::get($user_id);
        $map['mchToken'] = $this->mchToken;
        if(empty($user)){
            $this->error('用户信息不存在.');
        }
        if($user['status'] != 1){
            $this->error('实名用户才可以升级.');
        }
        if($this->request->isPost()){
            $identity = (int)$this->request->post('identity');
            if(db('users')->where(['user_id'=>$user_id])->where($map)->update(['identity'=>$identity])){
                $this->success('用户已经升级.','/reload');
            }
            $this->error('用户升级失败.');
        }
        $this->assign('user',$user);
        $this->assign('userLevel',UserModel::UserLevel());
        return $this->fetch();
    }
    /***********************************会员组***********************************/
    public function userGroup(){
        if(request()->isPost()){
            $userLevel=db('user_level');
            $list=$userLevel->order('sort')->select();
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list,'rel'=>1];
        }
        return $this->fetch();
    }
    public function groupAdd(){
        if(request()->isPost()){
            $data = input('post.');
            $data['open'] = input('post.open') ? input('post.open') : 0;
            db('user_level')->insert($data);
            $result['msg'] = '会员组添加成功!';
            $result['url'] = url('userGroup');
            $result['code'] = 1;
            return $result;
        }else{
            $this->assign('title',lang('add')."会员组");
            $this->assign('info','null');
            return $this->fetch('groupForm');
        }
    }
    public function groupEdit(){
        if(request()->isPost()) {
            $data = input('post.');
            db('user_level')->update($data);
            $result['msg'] = '会员组修改成功!';
            $result['url'] = url('userGroup');
            $result['code'] = 1;
            return $result;
        }else{
            $map['level_id'] = input('param.level_id');
            $info = db('user_level')->where($map)->find();
            $this->assign('title',lang('edit')."会员组");
            $this->assign('info',json_encode($info,true));
            return $this->fetch('groupForm');
        }
    }
    public function groupDel(){
        $level_id=input('level_id');
        if (empty($level_id)){
            return ['code'=>0,'msg'=>'会员组ID不存在！'];
        }
        db('user_level')->where(array('level_id'=>$level_id))->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function groupOrder(){
        $userLevel=db('user_level');
        $data = input('post.');
        $userLevel->update($data);
        $result['msg'] = '排序更新成功!';
        $result['url'] = url('userGroup');
        $result['code'] = 1;
        return $result;
    }
   

}